<template>
	<view>
		<u-navbar title="信息修改" titleStyle="color:#fff" left-icon-color="#fff" bgColor="#3083FB" :autoBack="true"
			placeholder>
		</u-navbar>
		<view class="bg"></view>
		<view class="main">
			<view class="avatar" v-show="type==1">
				<view class="title">
					<text @click="goBack(1)">取消</text>
					<text @click="goBack(2)">确定</text>
				</view>
				<u-upload :fileList="fileList1" width="200rpx" height="200rpx" @afterRead="afterRead" @delete="deletePic"
					name="1" :maxCount="4">
				</u-upload>
			</view>
			<view class="phone" v-show="type==0">

				<u--form labelPosition="left" :model="model1" ref="uForm">
					<u-form-item left-icon="phone" leftIconStyle="font-size:50rpx;color:#C9C9C9" prop="userInfo.name" borderBottom
						ref="item1">
						<u--input v-model="model1.userInfo.phone" border="none"></u--input>
					</u-form-item>

					<u-form-item left-icon="/static/images/code.png" leftIconStyle="width:50rpx;height:50rpx" prop="userInfo.code"
						borderBottom ref="item1">
						<u--input v-model="model1.userInfo.code" border="none"></u--input>
						<view class="wrap" slot="right">
							<u-toast ref="uToast"></u-toast>
							<!-- startText="张三" changeText="0X秒获取" -->
							<u-code :seconds="seconds" @end="end" @start="start" ref="uCode" @change="codeChange"></u-code>
							<u-button size="small" type="success" @tap="getCode">{{tips}}</u-button>
						</view>
					</u-form-item>
				</u--form>
				<view class="submit-btn">
					<u-button type="primary" @click="submit">提交</u-button>
				</view>
				
			</view>
		</view>
		<u-popup :show="show" :round="10" mode="center" :closeOnClickOverlay="false" @close="close" @open="open">
			<view class="popup">
				<text>手机号：{{model1.userInfo.phone}}</text>
				<text>验证码：{{model1.userInfo.code}}</text>
				<u-button type="primary" size="small" @click="close">关闭</u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				type: '',
				fileList1: [{
					url: 'http://www.img.ttblog.cn/car/user-icon.jpg'
				}, {
					url: 'http://www.img.ttblog.cn/car/user-icon.jpg'
				}, {
					url: 'http://www.img.ttblog.cn/car/user-icon.jpg'
				}, {
					url: 'http://www.img.ttblog.cn/car/user-icon.jpg'
				}],
				model1: {
					userInfo: {
						phone: '',
						code: '',
					},
				},
				show: false,

				tips: '张三',
				seconds: 10,
			};
		},
		onLoad(p) {
			this.type = p.type;
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			goBack(type) {
				if (type == 1) {
					// 取消
					uni.navigateBack();
				} else {
					// 确认
					uni.navigateBack();
				}
			},
			submit(e) {
				console.log(this.model1.userInfo);
				this.open();
			},
			open() {
				this.show = true;
			},
			close() {
				this.show = false;
			},

			codeChange(text) {
				// 修改提示语
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					console.log();
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 1000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			end() {
				// uni.$u.toast('倒计时结束');
			},
			start() {
				// uni.$u.toast('倒计时开始');
			}
		}
	}
</script>

<style lang="scss">
	.bg {
		background-color: #3083FB;
		height: 200rpx;
		margin-top: -2px;
	}

	.main{
		transform: translateY(-190rpx);
		padding: 20rpx;
		padding-top: 30rpx;
		.avatar,
		.phone {
			border-radius: 18rpx;
			background-color: #fff;
			padding: 0 20rpx;
		}
		
		.title {
			padding: 0 30rpx;
			height: 100rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
		}
		
		.submit-btn {
			margin-top: 50rpx;
		}
	}
	.popup {
		width: 500rpx;
		height: 300rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		border-radius: 20rpx;
		background-color: #fff;
		justify-content: space-around;
		/deep/ .u-button {
			width: 50%;
		}
	}
</style>